﻿using System;
using System.Collections.Generic;
using System.Text;
using CodeCreator.Template;

namespace CodeCreator
{
    public class Common
    {
        public static TypeMap GetTypeMapByDb(List<TypeMap> typeMapList,string dbType)
        {
            TypeMap result = null;
            foreach (TypeMap tm in typeMapList)
            {
                if (tm.DbType.ToUpper() == dbType.ToUpper())
                {
                    result = tm;
                    break;
                }
            }
            return result;
        }

        public static TypeMap GetTypeMapByCSharp(List<TypeMap> typeMapList, string cSharpType)
        {
            TypeMap result = null;
            foreach (TypeMap tm in typeMapList)
            {
                if (tm.CSharpType.ToUpper() == cSharpType.ToUpper())
                {
                    result = tm;
                    break;
                }
            }
            return result;
        }

        public static string StrFirstToLower(string str)
        {
            if (str.Length > 1)
            {
                str = str.Substring(0, 1).ToLower() + str.Substring(1, str.Length - 1);
            }
            else
            {
                str = str.ToLower();
            }
            return str;
        }

        public static string StrFirstToUpper(string str)
        {
            if (str.Length > 1)
            {
                str = str.Substring(0, 1).ToUpper() + str.Substring(1, str.Length - 1);
            }
            else
            {
                str = str.ToUpper();
            }
            return str;
        }


        public static string DbFirstToLower(string dbFieldName)
        {
            if (dbFieldName.IndexOf("_") >= 0)
            {
                string[] fs = dbFieldName.Split('_');
                List<string> resultList = new List<string>();
                string item;
                for (int i = 0; i < fs.Length;i++ )
                {
                    if (i == 0)
                    {
                        resultList.Add(fs[i].ToLower());
                    }
                    else
                    {
                        if (fs[i] != "")
                        {
                            if (fs[i].Length > 1)
                            {
                                item = fs[i].Substring(0, 1).ToUpper() + fs[i].Substring(1, fs[i].Length - 1).ToLower();
                            }
                            else
                            {
                                item = fs[i].ToUpper();
                            }
                            resultList.Add(item);
                        }
                    }
                }

                string result = "";
                foreach (string str in resultList)
                {
                    result += str;
                }
                return result;

            }
            else
                return dbFieldName.ToLower();
        }

        public static string DbFirstToUpper(string dbFieldName)
        {
            if (dbFieldName.IndexOf("_") >= 0)
            {
                string[] fs = dbFieldName.Split('_');
                List<string> resultList = new List<string>();
                string item;
                foreach (string str in fs)
                {
                    if (str != "")
                    {
                        if (str.Length > 1)
                        {
                            item = str.Substring(0, 1).ToUpper() + str.Substring(1, str.Length - 1).ToLower();
                        }
                        else
                        {
                            item = str.ToUpper();
                        }
                        resultList.Add(item);
                    }
                }

                string result = "";
                foreach (string str in resultList)
                {
                    result += str;
                }
                return result;

            }
            else
            {
                if (dbFieldName.Length > 1)
                {
                   return dbFieldName.Substring(0, 1).ToUpper() + dbFieldName.Substring(1, dbFieldName.Length - 1).ToLower();
                }
                else
                {
                    return dbFieldName.ToUpper();
                }
            }
        }
    }
}
